package com.asiainfo.dacp.datastash.broker.facade.state;

import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.asiainfo.dacp.datastash.broker.core.enums.UnitTaskStateEnum;
import com.asiainfo.dacp.datastash.broker.core.vo.StashUnitProcess;
import com.asiainfo.dacp.datastash.broker.core.vo.StashUnitTask;
import com.asiainfo.dacp.datastash.orm.dao.log.DataStashUnitTaskEventLogDao;
import com.asiainfo.dacp.datastash.orm.dao.state.DataStashUnitTaskStateLogDao;
import com.asiainfo.dacp.datastash.orm.po.log.DataStashUnitTaskEventLogPo;
import com.asiainfo.dacp.datastash.orm.po.state.DataStashUnitTaskStateLogPo;
import com.asiainfo.dacp.util.BeanConvertUtils;
import com.asiainfo.dacp.util.JsonHelper;
import com.google.common.base.Strings;

/**
 * 单元分片数据库状态操作
 * 
 * @author MeiKefu
 * @date 2017-11-30
 */
@Service
public class UnitTaskStateLogService {

	@Autowired
	private DataStashUnitTaskStateLogDao unitTaskStateLogDao;

	@Autowired
	private DataStashUnitTaskEventLogDao unitTaskEventLogDao;

	@Transactional
	public void create(List<StashUnitTask> unitTaskList) {
		if (unitTaskList != null && unitTaskList.size() > 0) {
			List<DataStashUnitTaskStateLogPo> result = new ArrayList<>();
			for (StashUnitTask unitTask : unitTaskList) {
				DataStashUnitTaskStateLogPo unitShardLogPo = new DataStashUnitTaskStateLogPo(unitTask.getTaskProcId(),
						unitTask.getUnitProcId(), unitTask.getUnitCode(), unitTask.getBatchNo(),
						UnitTaskStateEnum.INIT.value());
				String taskStepsStr = JsonHelper.getInstance().write(unitTask.getTaskSteps());
				unitShardLogPo.setTaskSteps(taskStepsStr);
				unitShardLogPo.setTaskType(unitTask.getTaskType());
				unitShardLogPo.setNextTaskProcIds(JsonHelper.getInstance().write(unitTask.getNextTaskProcIds()));
				unitShardLogPo.setPreTaskProcIds(JsonHelper.getInstance().write(unitTask.getPreTaskProcIds()));
				unitShardLogPo.setMessage("分片创建");
				unitShardLogPo.setTaskContent(JsonHelper.getInstance().write(unitTask.getTaskContent()));
				result.add(unitShardLogPo);
			}
			unitTaskStateLogDao.save(result);
			unitTaskEventLogDao.save(transform(result));
		}
	}
	
	public void update(StashUnitTask task, UnitTaskStateEnum state, String brokerId, String brokerIp){
		update(task.getTaskProcId(), task.getUnitProcId(), task.getUnitCode(), task.getBatchNo(), state.value()
				, state.label(), brokerId, brokerIp);
	}
	
	public void update(StashUnitTask task, UnitTaskStateEnum state){
		update(task.getTaskProcId(), task.getUnitProcId(), task.getUnitCode(), task.getBatchNo(), state.value()
				, state.label(), null, null);
	}
	
	public void update(StashUnitTask task, UnitTaskStateEnum state, String message,
			String brokerId, String brokerIp){
		update(task.getTaskProcId(), task.getUnitProcId(), task.getUnitCode(), task.getBatchNo(), state.value()
				, message, brokerId, brokerIp);
	}

	@Transactional
	public void update(String taskId, String unitProcId, String unitCode, String batchNo, String state, String message,
			String brokerId, String brokerIp) {
		DataStashUnitTaskStateLogPo unitShardLogPo = unitTaskStateLogDao.findTaskState(taskId, unitCode, batchNo);

		if (unitShardLogPo != null) {
			unitShardLogPo.setLastupd(GregorianCalendar.getInstance().getTime());
			unitShardLogPo.setState(state);
		} else {
			unitShardLogPo = new DataStashUnitTaskStateLogPo(taskId, unitProcId, unitCode, batchNo, state);
		}

		if (!Strings.isNullOrEmpty(message)) {
			unitShardLogPo.setMessage(message);
		}

		if (brokerId != null && brokerId.length() > 0) {
			unitShardLogPo.setBrokerId(brokerId);
		}

		if (brokerIp != null && brokerIp.length() > 0) {
			unitShardLogPo.setBrokerIp(brokerIp);
		}
		
		if (UnitTaskStateEnum.RUNNING.equals(state)) {
			unitShardLogPo.setStartDt(GregorianCalendar.getInstance().getTime());
		} else if (UnitTaskStateEnum.SUCESS.equals(state)) {
			unitShardLogPo.setCompleteDt(GregorianCalendar.getInstance().getTime());
		}

		unitTaskStateLogDao.save(unitShardLogPo);
		unitTaskEventLogDao.save(transform(unitShardLogPo));
	}

	private DataStashUnitTaskEventLogPo transform(DataStashUnitTaskStateLogPo unitTaskStatePo) {
		DataStashUnitTaskEventLogPo logPo = BeanConvertUtils.copyBean(unitTaskStatePo,
				DataStashUnitTaskEventLogPo.class);
		logPo.setCreateDt(new Date());
		return logPo;
	}

	private List<DataStashUnitTaskEventLogPo> transform(List<DataStashUnitTaskStateLogPo> unitTaskStatePoes) {
		return BeanConvertUtils.copyList(unitTaskStatePoes, DataStashUnitTaskEventLogPo.class);
	}
}
